//SPDX-FileCopyrightText: Copyright 2022-2024 深圳市同心圆网络有限公司
//SPDX-License-Identifier: GPL-3.0-only

package upgrade

import (
	"context"
	"fmt"

	"atomgit.com/openlinksaas/api-server/dao"
)

const (
	SCHEAM_VERSION = uint32(40)
)

func UpgradeDb() error {
	ctx := context.Background()
	exist, err := dao.VersionDao.Exist(ctx, dao.VERSION_KEY_SCHEMA)
	if err != nil {
		return err
	}
	if !exist {
		err = dao.VersionDao.Set(ctx, dao.VERSION_KEY_SCHEMA, 1)
		if err != nil {
			return err
		}
	}
	for {
		versionItem, err := dao.VersionDao.Get(ctx, dao.VERSION_KEY_SCHEMA)
		if err != nil {
			return err
		}
		if versionItem.Version == SCHEAM_VERSION {
			return nil
		}
		err = execUpgrade(ctx, versionItem.Version)
		if err != nil {
			return err
		}
	}
}

func execUpgrade(ctx context.Context, version uint32) error {
	if version > SCHEAM_VERSION {
		return fmt.Errorf("schema version is too high")
	}
	if version == 1 {
		uper := &UpToVer2{}
		err := uper.Exec(ctx)
		if err != nil {
			return err
		}
	} else if version == 2 {
		uper := &UpToVer3{}
		err := uper.Exec(ctx)
		if err != nil {
			return err
		}
	} else if version == 3 {
		uper := &UpToVer4{}
		err := uper.Exec(ctx)
		if err != nil {
			return err
		}
	} else if version == 4 {
		uper := &UpToVer5{}
		err := uper.Exec(ctx)
		if err != nil {
			return err
		}
	} else if version == 5 {
		uper := &UpToVer6{}
		err := uper.Exec(ctx)
		if err != nil {
			return err
		}
	} else if version == 6 {
		uper := &UpToVer7{}
		err := uper.Exec(ctx)
		if err != nil {
			return err
		}
	} else if version == 7 {
		uper := &UpToVer8{}
		err := uper.Exec(ctx)
		if err != nil {
			return err
		}
	} else if version == 8 {
		uper := &UpToVer9{}
		err := uper.Exec(ctx)
		if err != nil {
			return err
		}
	} else if version == 9 {
		uper := &UpToVer10{}
		err := uper.Exec(ctx)
		if err != nil {
			return err
		}
	} else if version == 10 {
		uper := &UpToVer11{}
		err := uper.Exec(ctx)
		if err != nil {
			return err
		}
	} else if version == 11 {
		uper := &UpToVer12{}
		err := uper.Exec(ctx)
		if err != nil {
			return err
		}
	} else if version == 12 {
		uper := &UpToVer13{}
		err := uper.Exec(ctx)
		if err != nil {
			return err
		}
	} else if version == 13 {
		uper := &UpToVer14{}
		err := uper.Exec(ctx)
		if err != nil {
			return err
		}
	} else if version == 14 {
		uper := &UpToVer15{}
		err := uper.Exec(ctx)
		if err != nil {
			return err
		}
	} else if version == 15 {
		uper := &UpToVer16{}
		err := uper.Exec(ctx)
		if err != nil {
			return err
		}
	} else if version == 16 {
		uper := &UpToVer17{}
		err := uper.Exec(ctx)
		if err != nil {
			return err
		}
	} else if version == 17 {
		uper := &UpToVer18{}
		err := uper.Exec(ctx)
		if err != nil {
			return err
		}
	} else if version == 18 {
		uper := &UpToVer19{}
		err := uper.Exec(ctx)
		if err != nil {
			return err
		}
	} else if version == 19 {
		uper := &UpToVer20{}
		err := uper.Exec(ctx)
		if err != nil {
			return err
		}
	} else if version == 20 {
		uper := &UpToVer21{}
		err := uper.Exec(ctx)
		if err != nil {
			return err
		}
	} else if version == 21 {
		uper := &UpToVer22{}
		err := uper.Exec(ctx)
		if err != nil {
			return err
		}
	} else if version == 22 {
		uper := &UpToVer23{}
		err := uper.Exec(ctx)
		if err != nil {
			return err
		}
	} else if version == 23 {
		uper := &UpToVer24{}
		err := uper.Exec(ctx)
		if err != nil {
			return err
		}
	} else if version == 24 {
		uper := &UpToVer25{}
		err := uper.Exec(ctx)
		if err != nil {
			return err
		}
	} else if version == 25 {
		uper := &UpToVer26{}
		err := uper.Exec(ctx)
		if err != nil {
			return err
		}
	} else if version == 26 {
		uper := &UpToVer27{}
		err := uper.Exec(ctx)
		if err != nil {
			return err
		}
	} else if version == 27 {
		uper := &UpToVer28{}
		err := uper.Exec(ctx)
		if err != nil {
			return err
		}
	} else if version == 28 {
		uper := &UpToVer29{}
		err := uper.Exec(ctx)
		if err != nil {
			return err
		}
	} else if version == 29 {
		uper := &UpToVer30{}
		err := uper.Exec(ctx)
		if err != nil {
			return err
		}
	} else if version == 30 {
		uper := &UpToVer31{}
		err := uper.Exec(ctx)
		if err != nil {
			return err
		}
	} else if version == 31 {
		uper := &UpToVer32{}
		err := uper.Exec(ctx)
		if err != nil {
			return err
		}
	} else if version == 32 {
		uper := &UpToVer33{}
		err := uper.Exec(ctx)
		if err != nil {
			return err
		}
	} else if version == 33 {
		uper := &UpToVer34{}
		err := uper.Exec(ctx)
		if err != nil {
			return err
		}
	} else if version == 34 {
		uper := &UpToVer35{}
		err := uper.Exec(ctx)
		if err != nil {
			return err
		}
	} else if version == 35 {
		uper := &UpToVer36{}
		err := uper.Exec(ctx)
		if err != nil {
			return err
		}
	} else if version == 36 {
		uper := &UpToVer37{}
		err := uper.Exec(ctx)
		if err != nil {
			return err
		}
	} else if version == 37 {
		uper := &UpToVer38{}
		err := uper.Exec(ctx)
		if err != nil {
			return err
		}
	} else if version == 38 {
		uper := &UpToVer39{}
		err := uper.Exec(ctx)
		if err != nil {
			return err
		}
	} else if version == 39 {
		uper := &UpToVer40{}
		err := uper.Exec(ctx)
		if err != nil {
			return err
		}
	}
	return nil
}
